//
//  PaperCardCollectionView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/3/15.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperCardCollectionView.h"
#import "BaseCollectionViewCell.h"
#import "CardsCollectionReusableView.h"

@implementation PaperCardCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self registerCellClass:@"PaperCardCollectionViewCell" cellIdentifier:@"PaperCardCollectionViewCellID"];
        [self registerClass:[CardsCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardsCollectionReusableViewID"];
    }
    return self;
}

#pragma mark  collectionView delegate dataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sourceData.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    PaperModuleModel *model = self.sourceData[section];
    return model.module_question.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return  CGSizeMake(42.0f, 42.0f);
}

//这个方法是返回 Header的大小 size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    PaperModuleModel *model = self.sourceData[section];
    return model.showModule == YES ? CGSizeMake(SCREEN_WIDTH, 32.0f) : CGSizeZero;
}

//这个也是最重要的方法 获取Header的 方法。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"CardsCollectionReusableViewID";
    CardsCollectionReusableView *head = (CardsCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    PaperModuleModel *model = self.sourceData[indexPath.section];
    head.title = model.module_name;
    return head;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:
                                    @"PaperCardCollectionViewCellID" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[BaseCollectionViewCell alloc] init];
    }
    
    PaperModuleModel *moduleModel = self.sourceData[indexPath.section];
    PaperAnswerModel *answerModel = moduleModel.module_question[indexPath.row];
    
    cell.count          = answerModel.ques_count;
    cell.indexPath      = indexPath;
    [cell bindModel:answerModel];
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
